package com.liuzy.main;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.UriBuilder;

import com.sun.grizzly.http.SelectorThread;
import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory;

/**
 * 导出Jar运行
 *
 * @author liuzy
 */
public class Main {
	public static void main(String[] args) throws IOException {
		int port = 80;
		try {
			if (args != null && args.length > 0)
				port = Integer.parseInt(args[0]);
		} catch (Exception e) {
			port = 8080;
		}

		final URI BASE_URI = UriBuilder.fromUri("http://127.0.0.1/").port(port).build();
		final Map<String, String> initParams = new HashMap<String, String>();
		initParams.put("com.sun.jersey.config.property.packages", "com.liuzy.resource");
		initParams.put("com.sun.jersey.api.json.POJOMappingFeature", "true");
		SelectorThread threadSelector = GrizzlyWebContainerFactory.create(BASE_URI, initParams);
		System.out.println(String.format("Jersey app started with WADL available at " + "%sapplication.wadl\nHit enter to stop it...", BASE_URI));

		new Thread(new Runnable() {
			public void run() {
				try {
					new URL(BASE_URI.toURL().toString() + "application.wadl").openStream().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}).start();

		System.in.read();
		threadSelector.stopEndpoint();
	}
}
